Lab 02 - Simulations and Packages

Courtney Stowers

Library

library(tidyverse)

Load previous functions

# Function to set-up game
start_game <- function() {
  
  # Create list of envelope stuffers
  envelope_stuffers <- c("newsprint", "$1000")
  for (i in 1:98) {
    #print(i)
    envelope_stuffers <- append(envelope_stuffers, "newsprint")
  }
  
  # Place them randomly in game envelopes
  game_envelopes <- sample(envelope_stuffers, size=100, replace = F)
  
  return(game_envelopes)
}

# Function for contestant's first choice
select_contestant_choice <- function() {
  choice <- sample(1:100, size=1, replace = F)
  return(choice)
}

# Function for host's choice
select_host_choice <- function(envelopes, contestant_choice) {
  
  # Update envelope options by eliminating contestant's choice
  host_envelopes <- envelopes[-contestant_choice]
  
  host_choice <- sample(which(host_envelopes == "newsprint"), size=98, replace = F)
  
  host_choice <- c(host_choice)
  
  return(host_choice)
  
}

# Contestant's final choice to keep original envelope or switch
final_contestant_choice <- function(envelopes, contestant_choice, host_choice, switch_choice) {
  
  final_envelope <- envelopes[-contestant_choice]
  final_envelope <- final_envelope[-host_choice]
  
  #print(switch_choice)
  
  if (toupper(switch_choice) == "SWITCH") {
    out <- final_envelope
  } else {
    out <- envelopes[contestant_choice]
  }
  
  return(out)
}

# Function to determine game outcome
game_outcome <- function(final_choice) {
  if (final_choice == "$1000") {
    return("WIN")
  } else {
    return("LOSE")
  }
}

# Game play
monty_hall_100doors <- function(switch_choice) {
  
  envelopes <- start_game()
  contestant_choice <- select_contestant_choice()
  host_choice <- select_host_choice(envelopes, contestant_choice)
  final_choice <- final_contestant_choice(envelopes, contestant_choice, host_choice, switch_choice)
  outcome <- game_outcome(final_choice)
  
  return(list(envelopes = envelopes, 
              contestant_choice = contestant_choice, 
              host_choice = host_choice, 
              switch_choice = switch_choice, 
              final_choice = final_choice, 
              outcome = outcome))
}

Review output

  • Recall that we get the following output from our function:
monty_hall_100doors("KEEP")
## $envelopes
##   [1] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##   [7] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [13] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [19] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [25] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [31] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [37] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [43] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [49] "$1000"     "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [55] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [61] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [67] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [73] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [79] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [85] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [91] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [97] "newsprint" "newsprint" "newsprint" "newsprint"
## 
## $contestant_choice
## [1] 62
## 
## $host_choice
##  [1] 28 43  3 25 44 64 85 92 48 87 84 50 91 38  1 74 68 63 89 17  7 72  6 36 14
## [26] 95 98 34 78  9 80  2 53 59 20 96 21 97 93  8 60 12 94  4 99 29 47 58 46 10
## [51] 45 66 40 73 55 54 27 76 77 65  5 67 69 86 52 11 24 57 41 79 70 62 33 18 88
## [76] 51 35 90 82 37 42 61 31 81 19 15 16 13 83 56 71 39 26 23 22 30 32 75
## 
## $switch_choice
## [1] "KEEP"
## 
## $final_choice
## [1] "newsprint"
## 
## $outcome
## [1] "LOSE"
monty_hall_100doors("SWITCH")
## $envelopes
##   [1] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##   [7] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [13] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [19] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [25] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [31] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [37] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [43] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [49] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [55] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [61] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [67] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [73] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [79] "newsprint" "newsprint" "newsprint" "newsprint" "$1000"     "newsprint"
##  [85] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [91] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [97] "newsprint" "newsprint" "newsprint" "newsprint"
## 
## $contestant_choice
## [1] 11
## 
## $host_choice
##  [1] 69 59 56 60 84 63 22 78 31 80 77 74 20 65 72 15 79 30 16 87 95 75  5 21 89
## [26] 43 42 34 96 19 13 91 23 48 62 51  6 99 39 70  9 98 41 14 12  2 53 54 29 92
## [51] 46 68 93 61 58 86 32 38 24 26 28 50 18  1 25 71 40 67 45  7 36 97 17 44 57
## [76] 76 64 55 85  8 73 37 10 94 52 49 81 27 33 35 11  3 88 66  4 83 47 90
## 
## $switch_choice
## [1] "SWITCH"
## 
## $final_choice
## [1] "$1000"
## 
## $outcome
## [1] "WIN"

Create play game function

With this new function, we want to play the entire game and save the outcome of keeping or switching.

play_game <- function( )
{
  outcome.stay <- monty_hall_100doors("KEEP")$outcome
  outcome.switch <- monty_hall_100doors("SWITCH")$outcome
  
  # game.results <- bundle the results
  # return( <<< game.results >>> )
  
  strategy <- c("stay","switch")
  outcome <- c(outcome.stay,outcome.switch)
  game.results <- data.frame( strategy, outcome,
                              stringsAsFactors=F )
  return( game.results )
}

play_game()
##   strategy outcome
## 1     stay    LOSE
## 2   switch     WIN

Simulate results of game

results.df <- NULL   # collector

for( i in 1:10000 )  # iterator
{
  game.outcome <- play_game()
  # binding step
  results.df <- rbind( results.df, game.outcome )
}

table( results.df ) 
##         outcome
## strategy LOSE  WIN
##   stay   9907   93
##   switch   91 9909
( table( results.df ) / 10000 ) %>% 
  round( 2 )
##         outcome
## strategy LOSE  WIN
##   stay   0.99 0.01
##   switch 0.01 0.99
play_n_games <- function( n=100 )
{
  
  library( dplyr )
  results.list <- list()   # collector
  loop.count <- 1

  for( i in 1:n )  # iterator
  {
    game.outcome <- play_game()
    results.list[[ loop.count ]] <- game.outcome 
    loop.count <- loop.count + 1
  }
  
  results.df <- dplyr::bind_rows( results.list )

  table( results.df ) %>% 
  prop.table( margin=1 ) %>%  # row proportions
  round( 2 ) %>% 
  print()
  
  return( results.df )

}

play_n_games()
##         outcome
## strategy LOSE  WIN
##   stay   0.98 0.02
##   switch 0.01 0.99
##     strategy outcome
## 1       stay    LOSE
## 2     switch     WIN
## 3       stay    LOSE
## 4     switch     WIN
## 5       stay    LOSE
## 6     switch     WIN
## 7       stay    LOSE
## 8     switch     WIN
## 9       stay    LOSE
## 10    switch     WIN
## 11      stay    LOSE
## 12    switch     WIN
## 13      stay    LOSE
## 14    switch     WIN
## 15      stay    LOSE
## 16    switch     WIN
## 17      stay    LOSE
## 18    switch     WIN
## 19      stay    LOSE
## 20    switch     WIN
## 21      stay    LOSE
## 22    switch     WIN
## 23      stay    LOSE
## 24    switch     WIN
## 25      stay    LOSE
## 26    switch     WIN
## 27      stay    LOSE
## 28    switch     WIN
## 29      stay    LOSE
## 30    switch     WIN
## 31      stay    LOSE
## 32    switch     WIN
## 33      stay    LOSE
## 34    switch     WIN
## 35      stay    LOSE
## 36    switch     WIN
## 37      stay    LOSE
## 38    switch     WIN
## 39      stay    LOSE
## 40    switch     WIN
## 41      stay    LOSE
## 42    switch     WIN
## 43      stay    LOSE
## 44    switch     WIN
## 45      stay    LOSE
## 46    switch     WIN
## 47      stay    LOSE
## 48    switch     WIN
## 49      stay    LOSE
## 50    switch     WIN
## 51      stay    LOSE
## 52    switch     WIN
## 53      stay    LOSE
## 54    switch     WIN
## 55      stay     WIN
## 56    switch     WIN
## 57      stay    LOSE
## 58    switch     WIN
## 59      stay    LOSE
## 60    switch     WIN
## 61      stay    LOSE
## 62    switch     WIN
## 63      stay    LOSE
## 64    switch     WIN
## 65      stay    LOSE
## 66    switch     WIN
## 67      stay    LOSE
## 68    switch     WIN
## 69      stay    LOSE
## 70    switch     WIN
## 71      stay    LOSE
## 72    switch     WIN
## 73      stay     WIN
## 74    switch     WIN
## 75      stay    LOSE
## 76    switch     WIN
## 77      stay    LOSE
## 78    switch     WIN
## 79      stay    LOSE
## 80    switch     WIN
## 81      stay    LOSE
## 82    switch     WIN
## 83      stay    LOSE
## 84    switch     WIN
## 85      stay    LOSE
## 86    switch     WIN
## 87      stay    LOSE
## 88    switch     WIN
## 89      stay    LOSE
## 90    switch     WIN
## 91      stay    LOSE
## 92    switch     WIN
## 93      stay    LOSE
## 94    switch     WIN
## 95      stay    LOSE
## 96    switch     WIN
## 97      stay    LOSE
## 98    switch     WIN
## 99      stay    LOSE
## 100   switch     WIN
## 101     stay    LOSE
## 102   switch     WIN
## 103     stay    LOSE
## 104   switch     WIN
## 105     stay    LOSE
## 106   switch     WIN
## 107     stay    LOSE
## 108   switch     WIN
## 109     stay    LOSE
## 110   switch     WIN
## 111     stay    LOSE
## 112   switch     WIN
## 113     stay    LOSE
## 114   switch     WIN
## 115     stay    LOSE
## 116   switch     WIN
## 117     stay    LOSE
## 118   switch     WIN
## 119     stay    LOSE
## 120   switch     WIN
## 121     stay    LOSE
## 122   switch     WIN
## 123     stay    LOSE
## 124   switch     WIN
## 125     stay    LOSE
## 126   switch     WIN
## 127     stay    LOSE
## 128   switch     WIN
## 129     stay    LOSE
## 130   switch     WIN
## 131     stay    LOSE
## 132   switch     WIN
## 133     stay    LOSE
## 134   switch     WIN
## 135     stay    LOSE
## 136   switch     WIN
## 137     stay    LOSE
## 138   switch     WIN
## 139     stay    LOSE
## 140   switch     WIN
## 141     stay    LOSE
## 142   switch     WIN
## 143     stay    LOSE
## 144   switch     WIN
## 145     stay    LOSE
## 146   switch     WIN
## 147     stay    LOSE
## 148   switch     WIN
## 149     stay    LOSE
## 150   switch     WIN
## 151     stay    LOSE
## 152   switch     WIN
## 153     stay    LOSE
## 154   switch     WIN
## 155     stay    LOSE
## 156   switch     WIN
## 157     stay    LOSE
## 158   switch     WIN
## 159     stay    LOSE
## 160   switch     WIN
## 161     stay    LOSE
## 162   switch     WIN
## 163     stay    LOSE
## 164   switch     WIN
## 165     stay    LOSE
## 166   switch     WIN
## 167     stay    LOSE
## 168   switch    LOSE
## 169     stay    LOSE
## 170   switch     WIN
## 171     stay    LOSE
## 172   switch     WIN
## 173     stay    LOSE
## 174   switch     WIN
## 175     stay    LOSE
## 176   switch     WIN
## 177     stay    LOSE
## 178   switch     WIN
## 179     stay    LOSE
## 180   switch     WIN
## 181     stay    LOSE
## 182   switch     WIN
## 183     stay    LOSE
## 184   switch     WIN
## 185     stay    LOSE
## 186   switch     WIN
## 187     stay    LOSE
## 188   switch     WIN
## 189     stay    LOSE
## 190   switch     WIN
## 191     stay    LOSE
## 192   switch     WIN
## 193     stay    LOSE
## 194   switch     WIN
## 195     stay    LOSE
## 196   switch     WIN
## 197     stay    LOSE
## 198   switch     WIN
## 199     stay    LOSE
## 200   switch     WIN